package com.lin.controller;


import com.lin.dataobject.ProductCategory;
import com.lin.server.ProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 卖家类目
 * Created by 廖师兄
 * 2017-07-23 21:06
 */
@Controller
//@ResponseBody
//@RestController
@RequestMapping("/category")
public class SellerCategoryController {

    @Autowired
    private ProductCategoryService categoryService;

    /**
     * 类目列表
     * @param map
     * @return
     */
    @RequestMapping("/list")
    public ModelAndView list(Map<String, Object> map) {
//        List<ProductCategory> categoryList = categoryService.findAll();
//        map.put("categoryList", categoryList);
        return new ModelAndView("category/index1", map);
    }

    @RequestMapping("/getData")
    @ResponseBody
    public Map<String,Object> getData(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                      @RequestParam(value = "rows", defaultValue = "10") Integer rows,
                                      HttpServletRequest request) {
        String name=request.getParameter("name");
        String startTime=request.getParameter("startTime");
        String endTime=request.getParameter("endTime");
        String type=request.getParameter("type");
        System.out.println(name+"   "+startTime+"      "+endTime+"    "+type);
        List<ProductCategory> categoryList = categoryService.findAll();
//        map.put("categoryList", categoryList);
        Map<String,Object> map=new HashMap<>();
        map.put("rows",categoryList);
        map.put("total",categoryList.size());
        return map;
//        return new ModelAndView("category/index1", map);
    }

    /**
     * 展示
     * @param categoryId
     * @param map
     * @return
     */
    @GetMapping("/index")
    public ModelAndView index(@RequestParam(value = "categoryId", required = false) Integer categoryId,
                              Map<String, Object> map) {
        if (categoryId != null) {
            ProductCategory productCategory = categoryService.findOne(categoryId);
            map.put("category", productCategory);
        }

        return new ModelAndView("category/index", map);
    }

//    /**
//     * 保存/更新
//     * @param form
//     * @param bindingResult
//     * @param map
//     * @return
//     */
//    @PostMapping("/save")
//    public ModelAndView save(@Valid CategoryForm form,
//                             BindingResult bindingResult,
//                             Map<String, Object> map) {
//        if (bindingResult.hasErrors()) {
//            map.put("msg", bindingResult.getFieldError().getDefaultMessage());
//            map.put("url", "/sell/seller/category/index");
//            return new ModelAndView("common/error", map);
//        }
//
//        ProductCategory productCategory = new ProductCategory();
//        try {
//            if (form.getCategoryId() != null) {
//                productCategory = categoryService.findOne(form.getCategoryId());
//            }
//            BeanUtils.copyProperties(form, productCategory);
//            categoryService.save(productCategory);
//        } catch (SellException e) {
//            map.put("msg", e.getMessage());
//            map.put("url", "/sell/seller/category/index");
//            return new ModelAndView("common/error", map);
//        }
//
//        map.put("url", "/sell/seller/category/list");
//        return new ModelAndView("common/success", map);
//    }
}
